package com.ias.agdis_r.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.StartActivity;
import com.ias.agdis_r.manager.ActionManager;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.sdcard.WriteLog;

/**
 * 程序开机自启服务
 * 
 * @author wanghu
 * 
 */
public class AutoStartBroadcastReceiver extends BroadcastReceiver {

	public static final String ROUTESERVICE_NET = "ias.routeservice.NET_STATUS_RECEIVER";
	public static final String GOTA_PTT_KEY = "com.ztegota.action.gotakey203";
	public static final String GOTA_SOS_KEY = "com.ztegota.action.gotakey202";
	public static final String NEOLINK_KEY = "com.neolink.action.filter.keyevent";

	@Override
	public void onReceive(Context context, Intent intent) {
		if(DataBaseManager.getInstance()==null)
		{
			new DataBaseManager(context.getApplicationContext());
		}

		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			WriteLog.getInstance().writeLog("AGDIS开机启动");
			//开机启动程序标识，默认值为true
			S.startActivity(StartActivity.class);
			return;
		} else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {

			ParameterModel.shutdownFlag = true;

			//关机广播，Provider状态置为为登录
			ActionManager.getInstance().getSecurityResponseAction().updateAccountStateProvider(null);

		} else if (intent.getAction().equals(ROUTESERVICE_NET)) {

			String netList = DataBaseManager.getInstance().getString(
					ParameterModel.T_NET_SELECT_INFO, ParameterModel.NET_LIST);
			// 10 --公网关闭
			// 11 --公网开启
			// 20 --专网关闭
			// 21 --专网开启
			// 30 --WIFI关闭
			// 31 --WIFI开启
			if (intent.getExtras().getString("PUBLIC_STATUS") != null) {
				System.out.println("公网状态:"
						+ intent.getExtras().getString("PUBLIC_STATUS"));
				WriteLog.getInstance()
						.writeLog(
								context.getString(R.string.pubState)
										+ intent.getExtras().getString(
										"PUBLIC_STATUS"));
				if (intent.getExtras().getString("PUBLIC_STATUS")
						.equals("OPEN")) {
					ServiceManage.getInstance().updateUsingNetStatus(11, intent.getExtras()
							.getString("PUBLIC_IP"));
				} else {
					ServiceManage.getInstance().updateUsingNetStatus(10, "");
					if (netList.contains(ParameterModel.PUBLIC_TAB_NUM)) {
//						EffectManager.getInstance().ttsPlay(
//								EffectManager.TTS_TYPE_UPTO, context.getString(R.string.pubDisconnect), false);
					}
				}
			} else if (intent.getExtras().getString("PRIVATE_STATUS") != null) {
				System.out.println("专网状态:"
						+ intent.getExtras().getString("PRIVATE_STATUS"));
				WriteLog.getInstance().writeLog(
						context.getString(R.string.priState)
								+ intent.getExtras()
								.getString("PRIVATE_STATUS"));
				if (intent.getExtras().getString("PRIVATE_STATUS")
						.equals("OPEN")) {
					ServiceManage.getInstance().updateUsingNetStatus(21, intent.getExtras()
							.getString("PRIVATE_IP"));
				} else {
					ServiceManage.getInstance().updateUsingNetStatus(20, "");
					if (netList.contains(ParameterModel.PRIVATE_TAB_NUM)) {
//						EffectManager.getInstance().ttsPlay(
//								EffectManager.TTS_TYPE_UPTO, context.getString(R.string.priDisconnect), false);
					}
				}
			} else if (intent.getExtras().getString("WIFI_STATUS") != null) {
				System.out.println("WIFI状态:"
						+ intent.getExtras().getString("WIFI_STATUS"));
				WriteLog.getInstance()
						.writeLog(
								context.getString(R.string.wifiState)
										+ intent.getExtras().getString(
												"WIFI_STATUS"));
				if (intent.getExtras().getString("WIFI_STATUS").equals("OPEN")) {
					ServiceManage.getInstance().updateUsingNetStatus(31, intent.getExtras()
							.getString("WIFI_IP"));
				} else {
					ServiceManage.getInstance().updateUsingNetStatus(30, "");
					if (netList.contains(ParameterModel.WIFI_TAB_NUM)) {
//						EffectManager.getInstance().ttsPlay(
//								EffectManager.TTS_TYPE_UPTO, context.getString(R.string.wifiDisconnect), false);
					}
				}
			}

			// 如果获得的信号强度,信号质量值不为空,写入日志
			WriteLog.getInstance().writeLog(
					context.getString(R.string.signalIntensity) + intent.getExtras().getFloat("PRIVATE_RSSI")
							+ context.getString(R.string.signalQuality)
							+ intent.getExtras().getFloat("PRIVATE_CINR"));
		}
		else if(GOTA_PTT_KEY.equals(intent.getAction()))
		{
			try {
				//按下对讲键 应用已经启动，且用户未登录
				if(ParameterModel.startMainActivityFlag)
				{
					if(!ServiceManage.getInstance().getIsLogon()) {
						//如果当前显示的不是登录页面
						if(!ParameterModel.startLoginActivityFlag) {
							//启动登录页面
							EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, context.getString(R.string.use_function_tologin), false);
							ServiceManage.getInstance().startLoginActivity();
						}
					}
				}
				else {
					//启动AGDIS应用
					S.startActivity(StartActivity.class);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
//		else if(GOTA_PTT_KEY.equals(intent.getAction())||NEOLINK_KEY.equals(intent.getAction()))
//		{
//			try {
//				//按下对讲键 应用已经启动，且用户未登录
//				if(ParameterModel.startMainActivityFlag)
//                {
//                    KeyEvent  event = (KeyEvent)intent.getParcelableExtra("action_event");
//                    if(event.getKeyCode()==232&&!ServiceManage.getInstance().getIsLogon()) {
//                        //如果当前显示的不是登录页面
//                        if(!ParameterModel.startLoginActivityFlag) {
//                            //启动登录页面
//							EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, context.getString(R.string.use_function_tologin), false);
//                            ServiceManage.getInstance().startLoginActivity();
//                            abortBroadcast();
//                        }
//                    }
//                }
//                else {
//                    //启动AGDIS应用
//                    S.startActivity(StartActivity.class);
//                    abortBroadcast();
//                }
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
	}
}
